package com.mail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mail.common.DataResult;
import com.mail.dto.EmailConfigDto;
import com.mail.dto.EmailConfigIdDto;
import com.mail.dto.SendEmailDto;
import com.mail.enums.ResponseCode;
import com.mail.mapper.EmailConfigMapper;
import com.mail.pojo.EmailConfig;
import com.mail.service.EmailConfigService;
import com.mail.utils.MailSendUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class EmailConfigServiceImpl extends ServiceImpl<EmailConfigMapper, EmailConfig> implements EmailConfigService {
    @Resource
    private EmailConfigMapper emailConfigMapper;

    @Override
    @Transactional
    public DataResult addEmailConfig(EmailConfigDto emailConfigDto){

        if(emailConfigDto.getUserId()==null || emailConfigDto.getEmailType()==null ||!StringUtils.isNotBlank(emailConfigDto.getAccountEmail()) || !StringUtils.isNotBlank(emailConfigDto.getEmailKey())||!StringUtils.isNotBlank(emailConfigDto.getEmailPort()) || !StringUtils.isNotBlank(emailConfigDto.getSendHost())){
            return DataResult.error(ResponseCode.STRING_TYPE_IS_NULL.getMsg());
        }
        Long id=emailConfigMapper.queryByAccountEmail(emailConfigDto.getAccountEmail());
        if(id!=null){
            return DataResult.error("已存在此邮箱！");
        }
        EmailConfig emailConfig=new EmailConfig(emailConfigDto.getUserId(), emailConfigDto.getAccountEmail(), emailConfigDto.getEmailType(), emailConfigDto.getEmailKey(),emailConfigDto.getEmailPort(),emailConfigDto.getSendHost());
        emailConfigMapper.insert(emailConfig);
        return DataResult.success("添加成功！");
    }

    @Override
    @Transactional
    public DataResult updateEmailConfig(Map<String, Object> map) {
        long id = Long.parseLong((String) map.get("id"));
        String accountEmail=(String) map.get("accountEmail");
        String emailKey=(String) map.get("emailKey");
        String emailType=(String) map.get("emailType");
        String userId1=(String) map.get("userId");
        String emailPort=(String) map.get("emailPort");
        String sendHost=(String) map.get("sendHost");
        UpdateWrapper<EmailConfig> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        if(StringUtils.isNotBlank(userId1)){
            long userId = Long.parseLong((String) map.get("userId"));
            wrapper.set("user_id", userId);
        }
        if(StringUtils.isNotBlank(emailType)){
            int emailType1 = Integer.parseInt((String) map.get("emailType"));
            wrapper.set("email_type", emailType1);
        }
        wrapper.set(StringUtils.isNotBlank(accountEmail), "account_email", accountEmail);
        wrapper.set(StringUtils.isNotBlank(emailKey), "email_key", emailKey);
        wrapper.set(StringUtils.isNotBlank(emailPort), "email_port", emailPort);
        wrapper.set(StringUtils.isNotBlank(sendHost), "send_host", sendHost);
        emailConfigMapper.update(null,wrapper);
        return DataResult.success("修改成功！");
    }

    @Override
    @Transactional
    public DataResult deleteEmailConfig(long id) {
        emailConfigMapper.deleteById(id);
        return DataResult.success("删除成功！");
    }

    @Override
    public DataResult queryEmailConfigPersonal(Map<String, Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int limit = Integer.parseInt((String) map.get("limit"));
        long id = Long.parseLong((String) map.get("id"));
        Page<EmailConfig> pageInfo = new Page<>(page, limit);
        QueryWrapper<EmailConfig> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",id);
        return DataResult.success(emailConfigMapper.selectPage(pageInfo,wrapper));
    }

    @Override
    public DataResult queryEmailByAccount(EmailConfigIdDto emailConfigIdDto) {
        EmailConfig emailConfig=emailConfigMapper.queryConfigByAccount(emailConfigIdDto.getAccountEmail());
        if(emailConfig==null){
            return DataResult.error(ResponseCode.EMAIL_CONFIG_ACCOUNT_IS_NULL.getMsg());
        }
//        SendEmailDto sendEmailDto=new SendEmailDto(
//                emailConfig.getEmailPort(),
//                emailConfigIdDto.getAccountEmail(),
//                emailConfig.getEmailKey(),
//                emailConfigIdDto.getTitle(),
//                emailConfigIdDto.getContent(),
//                emailConfigIdDto.getSendMailName(),
//                emailConfigIdDto.getFile(),
//                emailConfigIdDto.getReceiveMail(),
//                emailConfig.getSendHost(),
//                emailConfigIdDto.getReceiveMailName()
//                );
        SendEmailDto sendEmailDto=new SendEmailDto();
        sendEmailDto.setPort(emailConfig.getEmailPort());
        sendEmailDto.setSendMailPwd(emailConfig.getEmailKey());
        sendEmailDto.setSendSMTPHost(emailConfig.getSendHost());
        sendEmailDto.setContent(emailConfigIdDto.getContent());
        sendEmailDto.setFiles(emailConfigIdDto.getFiles());
        sendEmailDto.setSendMail(emailConfigIdDto.getAccountEmail());
        sendEmailDto.setTitle(emailConfigIdDto.getTitle());
        sendEmailDto.setSendMailName(emailConfigIdDto.getSendMailName());
        sendEmailDto.setReceiveMail(emailConfigIdDto.getReceiveMail());
        sendEmailDto.setReceiveMailName(emailConfigIdDto.getReceiveMailName());
        String msg="";
        if(sendEmailDto.getFiles()==null){
            msg= MailSendUtil.sendTextMail(sendEmailDto);
        }else{
            msg= MailSendUtil.sendFileMail(sendEmailDto);
        }
        if(msg==null) {
            return DataResult.success("发送成功！");
        }else{
            return DataResult.error(msg);
        }
    }
}
